package com.yxx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxx.controller.admin.MedicineCategoryController;
import com.yxx.mapper.ChineseWesternMedicineMapper;
import com.yxx.mapper.MedicineCategoryMapper;
import com.yxx.mapper.SetMedicineMapper;
import com.yxx.medicinecommon.constant.MessageConstant;
import com.yxx.medicinecommon.context.BaseContext;
import com.yxx.medicinecommon.exception.DeletionNotAllowedException;
import com.yxx.medicinecommon.result.PageResult;
import com.yxx.medicinepojo.dto.Medicine_CategoryDTO;
import com.yxx.medicinepojo.dto.Medicine_Category_PageQueryDTO;
import com.yxx.medicinepojo.entity.Medicine_Category;
import com.yxx.service.MedicineCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yuxiangxun
 * @date 2024/9/7
 * @apiNote
 */
@Service
@Slf4j
public class MedicineCategoryServiceImpl implements MedicineCategoryService {

    @Autowired
    private MedicineCategoryMapper medicineCategoryMapper;

    @Autowired
    private ChineseWesternMedicineMapper chineseWesternMedicineMapper;

    @Autowired
    private SetMedicineMapper setMedicineMapper;
    /**
     * 分页查询
     * @param medicineCategoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(Medicine_Category_PageQueryDTO medicineCategoryPageQueryDTO) {
        //开始分页
        PageHelper.startPage(medicineCategoryPageQueryDTO.getPage(),medicineCategoryPageQueryDTO.getPageSize());
        Page<Medicine_Category> page_Detail = medicineCategoryMapper.page(medicineCategoryPageQueryDTO);
        //PageResult pageResult = new PageResult();
        //pageResult.setTotal(page_Detail.getTotal());
        //pageResult.setRecords(page_Detail.getResult());
        //return pageResult;
        return new PageResult(page_Detail.getTotal(),page_Detail.getResult());
    }

    /**
     * 启用/禁用
     * @param status
     * @param id
     */
    @Override
    public void Category_Start_Disable(Integer status, Long id) {
        //根据当前线程获取当前用户id
        Long admin_id = BaseContext.getCurrentId();
        Medicine_Category medicineCategory = Medicine_Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(admin_id)
                .build();
        medicineCategoryMapper.update(medicineCategory);
    }

    /**
     * 修改分类信息
     * @param medicineCategoryDTO
     */
    @Override
    public void update_Category(Medicine_CategoryDTO medicineCategoryDTO) {
        Medicine_Category medicine_category = Medicine_Category.builder().build();
        BeanUtils.copyProperties(medicineCategoryDTO,medicine_category);
        medicineCategoryMapper.update(medicine_category);
    }

    /**
     * 根据类型查询分类列表
     * @param type
     * @return
     */
    @Override
    public List<Medicine_Category> typeList(Integer type) {
        List<Medicine_Category> typeList = medicineCategoryMapper.typeList_Select(type);
        log.info("typeList:{}",typeList.toString());
        return typeList;
    }

    /**
     * 新增分类
     * @param medicineCategoryDTO
     */
    @Override
    public void insert_Medicine_Category(Medicine_CategoryDTO medicineCategoryDTO) {
        //通过线程获取当前用户id
        Long admin_id = BaseContext.getCurrentId();
        Medicine_Category medicineCategory = new Medicine_Category();
        BeanUtils.copyProperties(medicineCategoryDTO,medicineCategory);
        medicineCategory.setStatus(0);
        medicineCategoryMapper.insert(medicineCategory);

    }

    /**
     * 根据分类id删除分类
     * @param id
     */
    @Override
    public void delete_Medicine_Category_id(Long id) {
        //查询当前分类是否关联了药品
        Integer ChineseWesternMedicine_count = chineseWesternMedicineMapper.countByMedicineCategory_id(id);
        if(ChineseWesternMedicine_count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_CHINESE_WESTERN_MEDICINE);
        }
        Integer SetMedicine_count = setMedicineMapper.countByMedicineCategory_id(id);
        if(SetMedicine_count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEDICINE_MEDICINE);
        }

        medicineCategoryMapper.delete_Medicine_Category_Id(id);
    }

    /**
     * 根据分类类型查询药品和组合药分类
     * @param type
     * @return
     */
    @Override
    public List<Medicine_Category> list(Integer type) {
        return medicineCategoryMapper.typeList_Select(type);
    }
}
